# -*- coding: utf-8 -*-

import requests
from datetime import datetime
from dingtalkchatbot.chatbot import DingtalkChatbot

url = "https://api.ip138.com/status/?token=43760a5e24d3668fc74cfcb95397885b"
# 获取当前时间


def nowTime():
    now = datetime.now()
    # 将时间格式化为字符串
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
    return formatted_time


# 设置 Webhook 地址
webhook = "https://oapi.dingtalk.com/robot/send?access_token=c54fa6837d584b0abc56c0a7d0456ca37d59213dcc7ea3f6f104b20de3c71284"
secret = "SEC9de9f15e7d9bc3be3c0c5f50adcf283ff26189940919486b9cdb0feda9ee45b1"
# 初始化机器人
bot = DingtalkChatbot(webhook=webhook, secret=secret)

# 获取 json
response = requests.get(url)
result = response.json()

# 判断 json 内容并做相关操作
if result["data"]["package"] < 10000:
    nowtime = nowTime()
    msg = "*{} IP138 iP 查询配额次数不足 10000，敬请关注！*".format(
        nowtime, result["data"]["package"]
    )
    # 将 print 的拼接内容作为消息发送给钉钉
    bot.send_text(msg)
